package filter

import (
	"context"
	"fmt"
	"github.com/hyperway/pkg/component"
	"github.com/hyperway/pkg/logger"
	"github.com/hyperway/pkg/transporter/dubbot"
)

type DubboConsumerTestFilter int

func (f DubboConsumerTestFilter) FilterId() string {
	return "dubbo-consumer-test"
}

func NewDubboConsumerTestFilter() *DubboConsumerTestFilter {
	return new(DubboConsumerTestFilter)
}

func (f *DubboConsumerTestFilter) DoFilter(next component.FilterInvoker) component.FilterInvoker {
	return func(ctx component.InnerContext) *component.ServerError {
		config, _ := dubbot.GetConsumerConfig("hello")
		values, err := dubbot.Encoder4Values(config.Types, "666")
		if err != nil {
			return component.NewServerError(500, "500", err.Error())
		}
		c, err := dubbot.NewContextWithConfig(
			config,
			context.TODO(),
			values,
			nil,
			logger.TraceId(ctx.RequestId()),
		)
		if err != nil {
			return component.NewServerError(500, "500", err.Error())
		}
		ret, e := dubbot.GetDubboInvoker().Invoke(c)
		if e != nil {
			return e
		}
		fmt.Printf("------------ret: %v\n", ret)
		return next(ctx)
	}
}
